using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace WindowsFormsDataBaice
{
    public partial class LabOrderForm : Form
    {
        private int visitId;
        private TextBox txtItemName;
        private NumericUpDown nudPrice;
        private Button btnConfirm;
        private Button btnCancel;

        public LabOrderForm(int visitId)
        {
            this.visitId = visitId;
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.Text = "开检验单";
            this.Size = new System.Drawing.Size(350, 200);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;

            // 检验项目名称
            var lblItemName = new Label 
            { 
                Text = "检验项目:", 
                Location = new System.Drawing.Point(20, 30), 
                Size = new System.Drawing.Size(80, 23) 
            };
            txtItemName = new TextBox 
            { 
                Location = new System.Drawing.Point(110, 30), 
                Size = new System.Drawing.Size(200, 23) 
            };

            // 费用
            var lblPrice = new Label 
            { 
                Text = "费用(元):", 
                Location = new System.Drawing.Point(20, 70), 
                Size = new System.Drawing.Size(80, 23) 
            };
            nudPrice = new NumericUpDown 
            { 
                Location = new System.Drawing.Point(110, 70), 
                Size = new System.Drawing.Size(200, 23),
                Minimum = 0,
                Maximum = 9999,
                DecimalPlaces = 2,
                Value = 0
            };

            // 按钮
            btnConfirm = new Button 
            { 
                Text = "确认开检验单", 
                Location = new System.Drawing.Point(150, 120), 
                Size = new System.Drawing.Size(100, 30),
                DialogResult = DialogResult.OK
            };
            btnCancel = new Button 
            { 
                Text = "取消", 
                Location = new System.Drawing.Point(260, 120), 
                Size = new System.Drawing.Size(60, 30),
                DialogResult = DialogResult.Cancel
            };

            // 事件绑定
            btnConfirm.Click += BtnConfirm_Click;

            // 添加控件
            this.Controls.AddRange(new Control[]
            {
                lblItemName, txtItemName,
                lblPrice, nudPrice,
                btnConfirm, btnCancel
            });

            // 预设一些常见检验项目
            txtItemName.Text = "血常规";
            nudPrice.Value = 25.00m;
        }

        private async void BtnConfirm_Click(object sender, EventArgs e)
        {
            // 验证输入
            if (string.IsNullOrWhiteSpace(txtItemName.Text))
            {
                MessageBox.Show("请输入检验项目名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtItemName.Focus();
                return;
            }

            if (nudPrice.Value <= 0)
            {
                MessageBox.Show("请输入检验费用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                nudPrice.Focus();
                return;
            }

            try
            {
                // 构建检验单数据
                var labOrderData = new
                {
                    visitId = this.visitId,
                    itemName = txtItemName.Text.Trim(),
                    price = nudPrice.Value
                };

                using (HttpClient client = new HttpClient())
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    string json = JsonConvert.SerializeObject(labOrderData);
                    var content = new StringContent(json, Encoding.UTF8, "application/json");

                    string url = "http://101.201.170.32:5166/api/DoctorWorkstation/add-laborder";
                    var response = await client.PostAsync(url, content);
                    string responseContent = await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        dynamic apiResponse = JsonConvert.DeserializeObject(responseContent);
                        if (apiResponse?.code == 200)
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            string errorMessage = apiResponse?.msg?.ToString() ?? "添加检验单失败";
                            MessageBox.Show(errorMessage, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show($"请求失败: {response.StatusCode}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"网络请求失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
} 